<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-02-25
 * Time: 16:39
 */
class Login extends MY_Controller{

    public function __construct(){
        parent::__construct();
    }

    public function do_login(){
        if($_REQUEST['submits']){
            $mobile = daddslashes(html_escape(strip_tags(trim($this->input->get_post("username")))));//username
            $password = (daddslashes(html_escape(strip_tags(trim($this->input->get_post("password"))))));//password
            $is_mobile = is_mobile($mobile);

            if(!$is_mobile || empty($password)){
                showMessage('index.php?c=login&m=do_login',-1,'手机号或者密码错误');
            }
//            $where = array("mobile" => $mobile, "status" => 0, "code" => $code);
//            $smsCode = $this->SmsCodeModel->getCode($where);
//            if(empty($smsCode)){
//                showMessage('index.php?c=login&m=do_login',-1,'验证码错误');
//            }
            //设置验证码过期
//            $param = array("status" => 1);
//            $this->SmsCodeModel->update_data($param, array("id"=>$smsCode["id"]));

            $result =  $this->UserModel->user_login($mobile,$password);
            if($result['status'] == 0){//登陆成功
                $dataCookie = array(
                    'uid'      => $result['data']['uid'],
                    'username' => $result['data']['username'],
                    'mobile'   => $result['data']['mobile'],
                    'nickname'=> $result['data']['nickname'],
                    'client_ip'=>get_client_ip(),
                );
                session_start();
                $loginUserKey = $this->config->config['loginUserKey'];
                setcookie($loginUserKey, encode_json($dataCookie),time() + 60*60*24*360);
                $this->AdminOpenationModel->insertLog($result['data']['uid'],22,'后台登陆成功：'.json_encode($dataCookie));
                header("Location:index.php");

            }else if($result['status'] == -1){//用户名、密码错误
                showMessage('index.php?c=login&m=do_login',-1,'手机号或者密码错误');

            }else if($result['status'] == -2){//没有权限
                showMessage('index.php?c=login&m=do_login',-1,'没有权限');
            }
        }
        $pageTitle = "后台登录";
        $this->load->view('login',array(
            "title" => $pageTitle,
        ));
    }

    public function send_code(){
        $mobile = daddslashes(html_escape(strip_tags(trim($this->input->get_post("mobile")))));
        if(!is_mobile($mobile)){
            std_out(-1, "手机号格式不正确","");
        }
        $result =  $this->UserModel->user_login($mobile);
        if(in_array($result['status'], array(-1, -2))){
            std_out(-1, "手机号不存在或者没有权限","");
        }
        $code = $this->SmsCodeModel->getNewCode($mobile);
        if($code == ''){
            std_out(-1, "短信验证码创建失败","");
        }
        $result = SyncSms::getInstance()->sendCode($mobile, $code);
        if($result["status"] == 0){
            std_out(0, "");
        }else{
            std_out(-1, $result["msg"]);
        }
    }
    /**
     * 退出登陆
     */
    public function login_out(){
        session_start();
        $loginUserKey = $this->config->config['loginUserKey'];
        $this->AdminOpenationModel->insertLog(1,23,'后台退出成功：'.json_encode($loginUserKey));
        setcookie($loginUserKey,"",time() - 60);
        header("Location:index.php?c=login&m=do_login");
    }
}